package com.example.util;

import com.example.model.TetrisNode;

import java.util.*;

public class TreeUtil {
    //获取所有子节点
    public static List<TetrisNode> getAllChildNode(TetrisNode node,boolean limit){
        List<TetrisNode> resultList = new ArrayList<>();
        Stack<TetrisNode> stack = new Stack<>();
        for(int i=0;i<node.turnTotal;i++){
            TetrisNode rootNode = (TetrisNode)node.clone();
            if(i!=0){
                for(int j=0;j<i;j++){
                    rootNode.turn();
                }
            }
            stack.add(rootNode);
        }
        while(!stack.isEmpty()){
            TetrisNode tetrisNode = (TetrisNode)stack.pop();
            if(tetrisNode.getBlockCount() - node.getBlockCount() >=1){
                continue;
            }
            //1.根据类型判断需要旋转几次
            //O(4) ---1
            //I(0),S(5),Z(6) ---2
            //L(1),J(2),T(3) ---4


            //1.左右滑移操作
            int leftMaxDistance = tetrisNode.getMaxLeftDistance();
            int rightMaxDistance = tetrisNode.getMaxRightDistance();
            for (int left = 0; left <= leftMaxDistance; left++) {

                TetrisNode leftTetrisNode = (TetrisNode) tetrisNode.clone();
                leftTetrisNode.left(left);
                leftTetrisNode.quickDown();
                //判断栈里有没有相同的，有就不加到栈里了
                boolean flag = true;
                for (int k = 0; k < resultList.size(); k++) {
                    if (resultList.get(k).equalWithNode(leftTetrisNode)) {
                        flag = false;
                        break;
                    }
                }
                if (flag) {
                    if(!leftTetrisNode.isGameOver){
                        resultList.add(leftTetrisNode);
                        stack.push(leftTetrisNode);
                    }
                }
            }
            //右边留一列以追求最高分
            if(limit){
                rightMaxDistance++;
            }
            for (int right =1; right < rightMaxDistance; right++) {
                TetrisNode rightTetrisNode = (TetrisNode) tetrisNode.clone();
                rightTetrisNode.right(right);
                rightTetrisNode.quickDown();
                //判断栈里有没有相同的，有就不加到栈里了
                boolean flag = true;
                for (int k = 0; k < resultList.size(); k++) {
                    if (resultList.get(k).equalWithNode(rightTetrisNode)) {
                        flag = false;
                        break;
                    }
                }
                if (flag) {
                    if(!rightTetrisNode.isGameOver){
                        resultList.add(rightTetrisNode);
                        stack.push(rightTetrisNode);
                    }

                }
            }
        }
        Iterator<TetrisNode> iterator = resultList.iterator();
        while(iterator.hasNext()){
            TetrisNode tetrisNode = (TetrisNode)iterator.next();
            tetrisNode.down();
            if(tetrisNode.isGameOver){
                iterator.remove();
            }
        }


        return resultList;
    }
}
